package com.mcj.managementSystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcj.managementSystem.common.Constants;
import com.mcj.managementSystem.controller.dto.UserDTO;
import com.mcj.managementSystem.entity.User;
import com.mcj.managementSystem.exception.ServiceException;
import com.mcj.managementSystem.mapper.UserMapper;
import com.mcj.managementSystem.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcj.managementSystem.utils.TokenUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 妙脆角
 * @since 2022-05-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
	
	private static final Log LOG = Log.get();
	
	@Override
	public UserDTO login(UserDTO userDTO) {
		User user = getUserInfo(userDTO);
		if (user != null) {
			BeanUtil.copyProperties(user, userDTO, true);
			// 设置token
			String token = TokenUtils.genToken(user.getId().toString(), user.getPassword());
			userDTO.setToken(token);
			return userDTO;
		} else {
			throw new ServiceException(Constants.CODE_600, "账号或密码错误");
		}
	}
	
	@Override
	public User register(UserDTO userDTO) {
		User user = getUserInfo(userDTO);
		if (user == null) {
			user = new User();
			BeanUtil.copyProperties(userDTO, user, true);
			save(user);  		// 把 copy完之后的用户对象存储到数据库
		} else {
			throw new ServiceException(Constants.CODE_600, "用户已存在");
		}
		return user;
	}
	
	private User getUserInfo(UserDTO userDTO) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("username", userDTO.getUsername());
		queryWrapper.eq("password", userDTO.getPassword());
		User user;
		try {
			user = getOne(queryWrapper); 		// 从数据库查询用户信息
		} catch (Exception e) {
			LOG.error(e);
			throw new ServiceException(Constants.CODE_500, "系统错误");
		}
		return user;
	}
}
